//
//  BookContentViewController.m
//  ZYTS-iPhone
//
//  Created by yuzhou on 12-12-10.
//  Copyright (c) 2012年 wzyk. All rights reserved.
//

#import "BookContentViewController.h"
#import "BookContentCell.h"
#import "PlayerViewController.h"
#import "AccountViewController.h"

@interface BookContentViewController ()

@end

@implementation BookContentViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotBookContent:) name:gotBookContentNotify object:nil];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    nothingView.hidden = YES;
    contentTableVIew.hidden = YES;
    [self showLoadingView];
    [[DataRequest sharedRequest] getBookContentByID:self.itemid];
}

- (void)gotBookContent:(NSNotification *)notification
{
    [self removeloadingView];
    if ([notification.object isKindOfClass:[NSDictionary class]]) {
        NSArray *result = [notification.object valueForKey:@"result"];
        if ([result isKindOfClass:[NSArray class]]) {
            self.contentArray = result;
            [contentTableVIew reloadData];
            if (_contentArray.count == 0) {
                contentTableVIew.hidden = YES;
                nothingView.hidden = NO;
            }
            else {
                contentTableVIew.hidden = NO;
                nothingView.hidden = YES;
            }
        }
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.contentArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BookContentCell *cell = [contentTableVIew dequeueReusableCellWithIdentifier:@"BookContentCell"];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"BookContentCell" owner:self options:nil] objectAtIndex:0];
    }
    cell.chapterIndex = indexPath.row;
    cell.chapterInfo = [self.contentArray objectAtIndex:indexPath.row];
    [cell updateDisplay];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (![[AuthorManager defaultManager] hasLoggedIn]) {
//        AccountViewController *accountViewController = [[[AccountViewController alloc] initWithNibName:@"AccountViewController" bundle:nil] autorelease];
//        accountViewController.needLogin = YES;
//        [self.view.window.rootViewController presentViewController:accountViewController animated:YES completion:nil];
//        return;
//    }
    
    [[AudioPlayer defaultPlayer] playlist:self.contentArray fromIndex:indexPath.row];
    
    //跳转到播放界面
    PlayerViewController *playerViewController = [[[PlayerViewController alloc] initWithNibName:@"PlayerViewController" bundle:nil] autorelease];
    UINavigationController *navController = (UINavigationController *)self.view.window.rootViewController;
    [navController pushViewController:playerViewController animated:YES];
}

- (void)showLoadingView
{
    if (loadingView.superview) {
        return;
    }
    if (loadingView == nil) {
        loadingView = [[[ABSpringLoadingView alloc] initWithFrame:self.view.bounds] autorelease];
    }
    loadingView.alpha = 0;
    [self.view addSubview:loadingView];
    [UIView animateWithDuration:0.3 animations:^{
        loadingView.alpha = 1;
    }];
}

- (void)removeloadingView
{
    if (loadingView) {
        [UIView animateWithDuration:0.3 animations:^{
            loadingView.alpha = 0;
        } completion:^(BOOL finished) {
            [loadingView removeFromSuperview];
            loadingView = nil;
        }];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [contentTableVIew release];
    [nothingView release];
    [super dealloc];
}
- (void)viewDidUnload {
    [contentTableVIew release];
    contentTableVIew = nil;
    [nothingView release];
    nothingView = nil;
    [super viewDidUnload];
}
@end
